package org.ysh.core.attachment;

import org.ysh.annotation.AdditionInfoResolver;
import org.apache.poi.xslf.usermodel.XMLSlideShow;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
@AdditionInfoResolver(extensions = {"pptx"})
public class Powerpoint2007AdditionInfoResolver implements AttachmentAdditionInfoResolver {

    @Override
    public void addAdditionInfo(File tmpFile, AdditionInfo additionInfo) throws Exception {
        try(XMLSlideShow xmlSlideShow = new XMLSlideShow(Files.newInputStream(tmpFile.toPath()));
            OutputStream os = Files.newOutputStream(tmpFile.toPath())){
            PoiHelper.writeCustomInfo(xmlSlideShow.getProperties().getCustomProperties(),additionInfo);
            xmlSlideShow.write(os);
        }

    }

    @Override
    public AdditionInfo readAdditionInfo(InputStream in) throws Exception {
        try(XMLSlideShow xmlSlideShow = new XMLSlideShow(in)){
            return PoiHelper.readAdditionInfo(xmlSlideShow.getProperties().getCustomProperties());
        }
    }

    public static void main(String[] args) throws Exception {
        Powerpoint2007AdditionInfoResolver powerpoint2007AdditionInfo = new Powerpoint2007AdditionInfoResolver();
        AdditionInfo additionInfo = new AdditionInfo();
        additionInfo.setUserName("Lisssi");
        additionInfo.setTime(new Date());
        additionInfo.setHost("192.168.1.1");
        powerpoint2007AdditionInfo.addAdditionInfo(new File("D:\\test.pptx"),additionInfo);

        additionInfo = powerpoint2007AdditionInfo.readAdditionInfo(Files.newInputStream(Paths.get("D:\\test.pptx")));
        System.out.println(additionInfo);
    }
}
